// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import Hero from "../hero";

const {ccclass, property} = cc._decorator;

@ccclass
export default class HeroSpine extends cc.Component {

  up: sp.Skeleton = null;

  down: sp.Skeleton = null;

  weapon: sp.Skeleton = null;

  boom: sp.Skeleton = null;

  float: sp.Skeleton = null;

  private march0: sp.Skeleton = null;
  private march1: sp.Skeleton = null;

  rush: sp.Skeleton = null;

  heroScript: Hero;

  private marchStop = false;

  onLoad() {
    this.up = this.node.getChildByName('up').getComponent(sp.Skeleton)
    this.down = this.node.getChildByName('down').getComponent(sp.Skeleton)
    this.weapon = this.node.getChildByName('w').getComponent(sp.Skeleton)
    this.boom = this.node.getChildByName('boom').getComponent(sp.Skeleton)
    this.float = this.node.getChildByName('float').getComponent(sp.Skeleton)
    this.march0 = this.node.getChildByName('march0').getComponent(sp.Skeleton)
    this.march1 = this.node.getChildByName('march1').getComponent(sp.Skeleton)
    this.march1.node.active = false;
    this.rush = this.node.getChildByName('rush').getComponent(sp.Skeleton)
    this.heroScript = this.getComponent(Hero)

    this.march0.setEventListener((trackEntry, e) => {
      if (this.march1.node.active) {
        return;
      }
      if (e.data.name === 'half' && !this.marchStop) {
        this.march1.node.active = true;
        this.march1.setAnimation(0, 'zs', true)
      }
    });
    this.float.setCompleteListener((trackEntry) => {
      this.float.node.active = false;
    })
  }

  stand() {
    this.up.setAnimation(0, 'stand', true)
    this.down.setAnimation(0, 'stand', true)
  }

  left() {
    this.up.setAnimation(0, 'left', true)
    this.down.setAnimation(0, 'left', true)
  }

  right() {
    this.up.setAnimation(0, 'right', true)
    this.down.setAnimation(0, 'right', true)
  }

  reset() {
    this.weapon.setAnimation(0, '1_', true);
    this.up.setAnimation(0, 'stand', true)
    this.down.setAnimation(0, 'stand', true)
  }

  die() {
    this.up.setAnimation(0, 'die', false)
    this.down.setAnimation(0, 'die', false)
    this.boom.setAnimation(0, 'mb', false)
    this.weapon.node.active = false;
  }

  born() {
    this.up.setAnimation(0, 'born', false);
    this.up.addAnimation(0, 'stand', true)
    this.down.setAnimation(0, 'born', false)
    this.down.addAnimation(0, 'stand', true)
    this.weapon.node.active = true;
    this.weapon.setAnimation(0, 'born', false)
    this.weapon.addAnimation(0, '1_', true)
  }

  upgradeToLevel(level: number) {
    const currentL = level - 1;
    if (currentL === 0) {
      this.weapon.setAnimation(0, '1_', true);
      return;
    }
    this.weapon.setAnimation(0, currentL + '_' + level, false);
    this.weapon.addAnimation(0, level + '_', true);
  }

  startMarch() {
    this.marchStop = false;
    this.march0.node.active = true;
    this.march0.setAnimation(0, 'zs', true)
  }

  startRush() {
    if (this.rush.node.active) {
      return;
    }
    this.rush.node.active = true;
    this.rush.setAnimation(0, 'zs', true)
  }

  stopMarch() {
    this.marchStop = true;
    this.march0.node.active = false;
    this.march1.node.active = false;
  }

  stopRush() {
    this.rush.node.active = false;
  }

  loadRole(roleId: number) {
    if (isNaN(roleId) || roleId == null) {
      roleId = 1;
    }
    cc.loader.loadRes(`spine/human/${roleId}/down/man`, sp.SkeletonData, (error: Error, resource: any) => {
      this.down.skeletonData = resource;
    });
    cc.loader.loadRes(`spine/human/${roleId}/up/man`, sp.SkeletonData, (error: Error, resource: any) => {
      this.up.skeletonData = resource;
      this.weapon.setEventListener((trackEntry, e) => {
        if (e.data.name === 'float') {
          this.float.node.active = true;
          this.float.setAnimation(0, 'zs', false);
        }
      })
    });
    this.float.setCompleteListener(trackEntry => {
      if (trackEntry.animation.name === 'zs') {
        this.float.node.active = false;
      }
    })
  }

  loadSwim(swimId: number, b: ClassDecorator) {
    if (isNaN(swimId) || swimId == null) {
      swimId = 1;
    }
    cc.loader.loadRes(`spine/swim-ring/${swimId}/e`, sp.SkeletonData, (error: Error, resource: any) => {
      this.weapon.skeletonData = resource;
      this.weapon.setAnimation(0, '1_', true)
    })
  }
}
